<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期" prop="supplier">
        <el-date-picker v-model="yearValue" type="year" value-format="yyyy" placeholder="选择年">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:IncomeStatement:Export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table :data="tableData" border="">
      <el-table-column prop="month" label="月份">
        <template slot-scope="scope">
          {{ formatMonth(scope.row.month) }}
        </template>
      </el-table-column>
      <el-table-column prop="inMoney" label="采购金额">
        <template slot-scope="scope">
          <span class="rightText">{{ scope.row.data ? formatNumber(scope.row.data[0] && scope.row.data[0].inMoney ?
      parseFloat(scope.row.data[0].inMoney).toFixed(2)
      : 0) : scope.row.totalInMoney > 0 ? formatNumber(parseFloat(scope.row.totalInMoney).toFixed(2)) : ''
            }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="outMoney" label="销售金额">
        <template slot-scope="scope">
          <span class="rightText">{{ scope.row.data ? formatNumber(scope.row.data[0] && scope.row.data[0].outMoney ?
      parseFloat(scope.row.data[0].outMoney).toFixed(2) :
      0) : scope.row.totalOutMoney > 0 ? formatNumber(parseFloat(scope.row.totalOutMoney).toFixed(2)) : ''
            }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="outMoney" label="利润金额">
        <template slot-scope="scope">
          <span class="rightText">{{ scope.row.data ? formatNumber(scope.row.data[0] && scope.row.data[0].inMoney &&
      scope.row.data[0].outMoney ? parseFloat(scope.row.data[0].outMoney - scope.row.data[0].inMoney).toFixed(2) :
      0) : scope.row.totalOutMoney -
        scope.row.totalInMoney > 0 ? formatNumber(parseFloat(scope.row.totalOutMoney -
          scope.row.totalInMoney).toFixed(2)) : ''
            }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>

</template>

<script>
import { listProfitMoney } from "@/api/system/IncomeStatement";
export default {
  name: 'IncomeStatement',
  data() {
    return {
      tableData: [],
      queryParams: {
        company: "",
        time: ''
      },
      showSearch: true,
      // 遮罩层
      loading: true,

      yearValue: new Date().getFullYear().toString()
    }
  },
  mounted() {
    this.getList();

  },
  methods: {
    // 列表数据
    getList() {
      this.loading = true;
      this.queryParams.time = this.yearValue;
      listProfitMoney(this.queryParams.time).then(response => {
        this.tableData = response.data;
        this.loading = false;
      })
    },
    // 搜索
    handleQuery() {
      this.getList();
    },
    // 重置
    resetQuery() {
      this.yearValue = new Date().getFullYear().toString()
      this.getList();
    },
    // 合计数
    formatMonth(month) {
      if (month) {
        // 清除月份前面的0
        return parseInt(month, 10).toString();
      } else {
        return '合计';
      }

    },
    // 数字转换
    formatNumber(value) {
      if (value) {
        let [integerPart, decimalPart] = value.toString().split('.');
        let formattedNumber = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',') + '.' + decimalPart.slice(0, 3);
        return formattedNumber;
      }

    },
    // 导出
    handleExport() { },



  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-table thead.is-group th {
  background-color: #f8f8f9;
}

::v-deep .el-table__cell {
  text-align: center;
}

.rightText {
  float: right;
}
</style>
